﻿using System.Windows;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.UnityExtensions;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Regions;
using System;
using Microsoft.Practices.Composite.Logging;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Composite.Presentation.Regions;
using Microsoft.Practices.Unity;
//using MP3Ganizer.Modules.FileView.Module;
using MP3Ganizer.Modules.DirectoryLoader;
using MP3Ganizer.Modules.Visualisation.Module;
using MP3Ganizer.Controls.Avalon;
using MP3Ganizer.Modules.MassEditor;
using MP3Ganizer.Modules.FileTree;
using MP3Ganizer.Modules.Infrastructure;
using MP3Ganizer.Modules.FileList;
// TODO: Add USING for each module
// <example>using MP3Ganizer.Modules.MyFunctionality.Module;</example>
//using MP3Ganizer.Modules.MyFunctionality.Module;
//using MP3Ganizer.Modules.FileView.Module;
//using MP3Ganizer.Modules.ModuleTwo.Module;

namespace MP3Ganizer.Prism
{
  /// <summary>
  /// Classe permettant de définir une version spécialisée du UnityBootstrapper
  /// </summary>
  public partial class PrismBootstrapper : UnityBootstrapper
  {
    #region Fields
    /// <summary>
    /// Utilisé pour logger les évènements levés par le UnityBootstrapper
    /// </summary>
    #endregion
    #region Override
    /// <summary>
    /// Fonction appelée par UnityBootstrapper permettant de définir le catalogue des modules et
    /// le moment où la fonction "Initialize" de chaque module est appelée.
    /// </summary>
    /// <returns>IModuleCatalog</returns>
    /// <remarks>TODO: Add module in ModuleCatalog</remarks>
    /// <example>catalog.AddModule(typeof(MyFunctionalityModule), InitializationMode.WhenAvailable);</example>
    protected override IModuleCatalog GetModuleCatalog()
    {
      ModuleCatalog catalog = new ModuleCatalog();
      // TO DO: Add here your code.
      catalog.AddModule(typeof(InfrastructureModule), InitializationMode.WhenAvailable);
      //catalog.AddModule(typeof(FileViewModule), InitializationMode.WhenAvailable);
      catalog.AddModule(typeof(DirectoryLoaderModule), InitializationMode.WhenAvailable);
      catalog.AddModule(typeof(VisualisationModule), InitializationMode.WhenAvailable);
      catalog.AddModule(typeof(MassEditorModule), InitializationMode.WhenAvailable);
      catalog.AddModule(typeof(FileTreeModule), InitializationMode.WhenAvailable);
      catalog.AddModule(typeof(FileListModule), InitializationMode.WhenAvailable);
      return catalog;
    }
    /// <summary>
    /// Fonction appelée par UnityBootstrapper permettant d'initialiser les modules
    /// dont le "InitializationMode" est à "WhenAvailable".
    /// </summary>
    protected override void InitializeModules()
    {
      IModuleManager _moduleManager = this.Container.Resolve<IModuleManager>();
      IModuleCatalog _moduleCatalog = this.Container.Resolve<IModuleCatalog>();
      foreach (ModuleInfo mi in _moduleCatalog.Modules)
      {
        if (mi.InitializationMode == InitializationMode.WhenAvailable)
          _moduleManager.LoadModule(mi.ModuleName);
      }
    }
    /// <summary>
    /// Fonction appelée par UnityBootstrapper permettant d'enregister des types dans
    /// le UnityContainer. Au minimum, le type du Shell.
    /// </summary>
    protected override void ConfigureContainer()
    {
      this.Container.RegisterType<IShellView, Shell>();

      base.ConfigureContainer();
    }
    /// <summary>
    /// Fonction appelée par UnityBootstrapper renvoyant une instance du Shell.
    /// </summary>
    /// <returns>DependencyObject Shell</returns>
    protected override DependencyObject CreateShell()
    {
      ShellPresenter presenter = Container.Resolve<ShellPresenter>();
      IShellView view = presenter.View;

      view.ShowView();

      return view as DependencyObject;
    }
    /// <summary>
    /// Fonction appelée par UnityBootstrapper permettant d'ajouter des RegionAdapters.
    /// </summary>
    /// <remarks>Un RegionAdapter permet de transformer un Control en conteneur de Region.</remarks>
    /// <returns>RegionAdapterMappings</returns>
    /// <example>mappings.RegisterMapping(typeof(Grid), this.Container.Resolve&lt;GridRegionAdapter>());</example>
    //protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
    //{
    //    RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();
    //    IRegionBehaviorFactory factory = base.ConfigureDefaultRegionBehaviors();
    //    if (regionAdapterMappings != null)
    //    {
    //        regionAdapterMappings.RegisterMapping(typeof(AvalonDock.DockablePane), new AvalonPaneRegionAdapter<AvalonDock.DockablePane, AvalonDock.DockableContent>(factory));
    //        regionAdapterMappings.RegisterMapping(typeof(AvalonDock.DocumentPane), new AvalonPaneRegionAdapter<AvalonDock.DocumentPane, AvalonDock.DocumentContent>(factory));
    //    }
    //    return regionAdapterMappings;
    //}
    #endregion
  }
}
